<?php

namespace Home\Controller;

class AreaController extends CommonController {

    public function lists() {
      
        $id = I('get.id', '0', 'int');
        if ($id > 0) {
            cookie("area", $id);
        }
        $sql = "(FIND_IN_SET('" . $id . "',circles) or circles =0) AND is_check = 1";
        $lists = M('shop')->where($sql)->select();    //计算总数
        foreach ($lists as $k => $v) {
            $lists[$k]['shopCla'] = getShopCla($v['business_time'], $v['weeks'], $v['is_busy']);
        }

        $says = M("say")->where("is_index = 1 AND pid = 0")->select();
        foreach ($says as $k => $v) {
            $says[$k]['sub'] = M("say")->where("is_index = 1 AND pid = " . $v['id'] . "")->find();
        }
        $this->assign("says", $says);
        $this->assign("ctitle", getAreaName(0));
        $this->assign("adsIndexBanner", getTableFileLists('ads', 1));
//        $this->assign("plays", getTableFile('play'));
        $this->assign("lists", $lists);

        $config = getTableConfig();
        $this->assign("ads_index_banner_height", getSubField("ads_cat", 1, 'height'));
        $this->display();
        /*         * *******服务********** */
        if ($config['is_start_service'] > 0) {
            $services = getTableFile('service');
            foreach ($services as $v) {
                if ($v['pid'] == 0) {
                    $servicesArr[] = $v;
                }
            }
            foreach ($servicesArr as $k => $v) {
                foreach ($services as $k2 => $v2) {
                    if ($v2['pid'] == $v['id']) {
                        $servicesArr[$k]['sub'][$k2] = $v2;
                    }
                }
            }
            $this->assign("servicesArr", $servicesArr);
            $this->display("Service:" . $config['is_start_service'] . "");
        }
        /*         * ***************** */
    }

    public function detail() {
        include_once("common/cart.php");
        $this->assign("ctitle", "" . $detail['name'] . "");
        $this->display();
        $config = getTableConfig();
        if ($config['cart_type'] == 1) {
            $modal = 'cart_more';
        } else {
            $modal = 'cart_single';
        }
        $this->display("Box:" . $modal . "");
    }

    public function search() {
        include_once("common/cart.php");
        $keyword = I("get.keyword");
        if ($keyword == '搜索餐厅、美食') {
            $keyword = '';
        }
        if ($keyword) {
            $shops = M("shop")->where("name like '%" . $keyword . "%'")->select();
            $goods = M("goods")->where("name like '%" . $keyword . "%'")->select();
            foreach ($goods as $v) {
                $sid[] = $v['sid'];
            }
            $total = count($shops) + count($goods);
            if ($sid) {
                $sid = array_unique($sid);
                foreach ($sid as $k => $v) {
                    $sid2[] = $v;
                }

                foreach ($sid2 as $k => $v) {
                    $arr[$k]['sid'] = $v;
                    $info = M("shop")->field("star,name,logo")->where("id = " . $v . "")->find();
                    $arr[$k]['name'] = $info['name'];
                    $arr[$k]['logo'] = $info['logo'];
                    $arr[$k]['id'] = $v;
                    $arr[$k]['star'] = getStar($info['star']);
//                $arr[$k]['state'] = getShopState($info['business_time'],$info['weeks'],$info['is_busy']);
//                $arr[$k]['cla'] = getShopCla($info['business_time'],$info['weeks'],$info['is_busy']);
                    $num = 0;
                    foreach ($goods as $k2 => $v2) {
                        if ($v2['sid'] == $v) {
                            $arr[$k]['goods'][$k2] = array(
                                "name" => $v2['name'],
                                "price" => $v2['price'],
                                "discount" => $v2['discount'],
                                "gid" => $v2['id']
                            );
                            $num++;
                        }
                    }
                    $arr[$k]['num'] = $num;
                }
            }
//           print_r($cartsArr);
            $this->assign("arr", $arr);
            $this->assign("shops", $shops);
            $this->assign("keyword", $keyword);
            $this->assign("total", $total);
        }
        $says = M("say")->where("is_index = 1 AND pid = 0")->select();
        foreach ($says as $k => $v) {
            $says[$k]['sub'] = M("say")->where("is_index = 1 AND pid = " . $v['id'] . "")->find();
        }
        $this->assign("says", $says);

        $this->assign("ctitle", "搜 索");
        $this->display();
        $config = getTableConfig();
        if ($config['cart_type'] == 1) {
            $modal = 'cart_more';
        } else {
            $modal = 'cart_single';
        }
        $this->display("Box:" . $modal . "");
    }

    public function messages() {
        $id = I("get.id", 0, 'int');
        $sql = "sid = " . $id . " AND pid = 0";
        $count = M('messages')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, 10);
        $lists = M('messages')->field("id,uid,content,addtime")->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order("id desc")->select();

        foreach ($lists as $k => $v) {
            $lists[$k]['sub'] = M('messages')->field("id,uid,uid2,content,addtime")->where("pid = " . $v['id'] . "")->order("id desc")->select();
        }
        //echo M('messages')->getLastSql();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        include_once("common/cart.php");
        $mtype = I("get.mtype", 0, 'int');
        if ($mtype > 0) {
            $order = M('order')->field("id,ordernum")->where("id = " . $mtype . " AND uid = " . session("userid") . "")->find();
            if ($order['id'] > 0) {
                $str_goods = '';

                $sub = M('order_goods')->field("gname,gnum")->where("pid = " . $order['id'] . "")->select();
                $num = count($sub);
                foreach ($sub as $k2 => $v2) {
                    if ($num != $k2 + 1) {
                        $dunhao = "、";
                    } else {
                        $dunhao = "";
                    }
                    $str_goods .= $sub[$k2]['gname'] . $sub[$k2]['gnum'] . "份" . $dunhao . "";
                }
                $messages = "您好，我的订单麻烦快一下，订单号" . $order['ordernum'] . "，订单内容:" . $str_goods . "，谢谢。";
                $this->assign("messages", $messages);
            }
        }
        $this->assign("ctitle", "餐厅留言");
        $this->display();
    }

    public function comments() {
        include_once("common/cart.php");
        $id = I("get.id", 0, 'int');
        $sql = "sid = " . $id . "";
        $count = M('comment')->where($sql)->count();    //计算总数
        $Page = new \Think\Page($count, 10);
        $lists = M('comment')->where($sql)->limit($Page->firstRow . ',' . $Page->listRows)->order("addtime desc")->select();
        foreach ($lists as $k => $v) {
            $lists[$k]['sub'] = M("order_goods")->field("gname,gnum")->where("pid = " . $v['id'] . "")->select();
        }
        //echo $Dao->getLastSql();
        $page = $Page->show();
        $this->assign("page", $page);
        $this->assign("lists", $lists);
        $this->assign("ctitle", "累积评价");
        $this->display();
    }

}

?>
